package com.narbeshuber.tablet.lasalara.fragment;

import java.util.List;

import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.narbeshuber.tablet.lasalara.LasaLaraActivity;
import com.narbeshuber.tablet.lasalara.R;
import com.narbeshuber.tablet.lasalara.Utils;
import com.narbeshuber.tablet.lasalara.dialog.AddBookPrompt;
import com.narbeshuber.tablet.lasalara.fragment.InfoFragment.LayoutType;
import com.narbeshuber.tablet.lasalara.http.AddBookTask;
import com.narbeshuber.tablet.lasalara.http.GetChaptersTask;
import com.narbeshuber.tablet.lasalara.listadapters.BookAdapter;
import com.narbeshuber.tablet.lasalara.listadapters.ChapterAdapter;
import com.narbeshuber.tablet.lasalara.model.Book;
import com.narbeshuber.tablet.lasalara.model.Chapter;

public class BookFragment extends ListFragment {
	
	private static BookFragment bookFragment;
	
	public static boolean deleteMode = false;
	public static boolean updateMode = false;
	
	public BookFragment(){
	}
	
	public static BookFragment getInstance(){
		if(bookFragment == null){
			bookFragment = new BookFragment();
		}
		return bookFragment;
	}
	
	public BookAdapter getListAdapter(){
		return (BookAdapter)super.getListAdapter();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.booklayout, container, false);
		setHasOptionsMenu(true);
		return v;
	}

	@Override
	public void onStart() {
		getActivity().invalidateOptionsMenu();
		ActionBar actionBar = getActivity().getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
    	actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, ActionBar.DISPLAY_SHOW_HOME);
		super.onStart();
	};

	/*@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.lasamenu, menu);
		//super.onCreateOptionsMenu(menu, inflater);
	}*/

	@Override
	public void onPrepareOptionsMenu(final Menu menu) {
		MenuItem item = menu.findItem(R.id.update_library);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				MenuItem infoitem = menu.findItem(R.id.info);
				infoitem.setIcon(R.drawable.ic_menu_info);
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					((LasaLaraActivity)getActivity()).hideInfo();
				}
				updateMode = !updateMode;
				if (updateMode) {
					item.setIcon(R.drawable.ic_menu_refresh_active);
				} else {
					item.setIcon(R.drawable.ic_menu_refresh);
				}
				getListAdapter().refreshList();
				return true;
			}
		});
		item = menu.findItem(R.id.night_mode);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				MenuItem infoitem = menu.findItem(R.id.info);
				infoitem.setIcon(R.drawable.ic_menu_info);
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					((LasaLaraActivity)getActivity()).hideInfo();
				}
				LasaLaraActivity.ninjaMode = !LasaLaraActivity.ninjaMode;
				if(LasaLaraActivity.ninjaMode){
					((LasaLaraActivity)getActivity()).setNightMode();
					item.setIcon(R.drawable.ic_menu_nightmode_active);
				} else {
					((LasaLaraActivity)getActivity()).setDayMode();
					item.setIcon(R.drawable.ic_menu_nightmode);
				}
				getListAdapter().notifyDataSetChanged();
				return true;
			}
		});
		item = menu.findItem(R.id.delete_books);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				MenuItem infoitem = menu.findItem(R.id.info);
				infoitem.setIcon(R.drawable.ic_menu_info);
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					((LasaLaraActivity)getActivity()).hideInfo();
				}
				deleteMode = !deleteMode;
				if (deleteMode) {
					item.setIcon(R.drawable.ic_menu_delete_active);
				} else {
					item.setIcon(R.drawable.ic_menu_delete);
				}
				getListAdapter().refreshList();
				return true;
			}
		});
		
		item = menu.findItem(R.id.add_book);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				MenuItem infoitem = menu.findItem(R.id.info);
				infoitem.setIcon(R.drawable.ic_menu_info);
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					((LasaLaraActivity)getActivity()).hideInfo();
				}
				return false;
			}
		});
		item = menu.findItem(R.id.info);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
		
			public boolean onMenuItemClick(MenuItem item) {
				FragmentManager fragmentManager = getFragmentManager();
		        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				if (getActivity().findViewById(R.id.right_top).getVisibility() == View.GONE){
			        fragmentTransaction.replace(R.id.right_top, new InfoFragment(LayoutType.BOOK));
		        	fragmentTransaction.commit();
		        	getActivity().findViewById(R.id.right).setVisibility(View.GONE);
		        	getActivity().findViewById(R.id.right_top).setVisibility(View.VISIBLE);
		        	item.setIcon(R.drawable.ic_menu_info_active);
		        	return true;
				} else {
					fragmentTransaction.remove(fragmentManager.findFragmentById(R.id.right_top));
		        	fragmentTransaction.commit();
		        	getActivity().findViewById(R.id.right).setVisibility(View.VISIBLE);
		        	getActivity().findViewById(R.id.right_top).setVisibility(View.GONE);
		        	item.setIcon(R.drawable.ic_menu_info);
					return true;
				}
			}
		
		});
		
		item = menu.findItem(R.id.sponsor);
		item.setEnabled(true);
		item.setVisible(true);
		//super.onPrepareOptionsMenu(menu);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		((LasaLaraActivity)getActivity()).exitOrLogOut = false;
		if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
			((LasaLaraActivity)getActivity()).hideInfo();
		}
		Book book = (Book) getListAdapter().getItem(position);

		List<Chapter> chapters = ((LasaLaraActivity) getActivity())
				.getDbHelper().getAllChaptersFromBook(book.getKey());
		((LasaLaraActivity) getActivity()).getPrefsHelper().saveBookKey(book.getKey());
		if (chapters.isEmpty()) {
			if(Utils.isOnline((LasaLaraActivity)getActivity())){
				new GetChaptersTask((LasaLaraActivity) getActivity()).execute(book
						.getKey());
			}
		} else {

			FragmentManager fragmentManager = (FragmentManager) getActivity()
					.getFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();
			ChapterFragment chapterFragment = ChapterFragment.getInstance();
			chapterFragment.setListAdapter(new ChapterAdapter(
					(LasaLaraActivity) getActivity(), chapters));
			fragmentTransaction
					.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			fragmentTransaction.replace(R.id.left, chapterFragment);
        	getActivity().findViewById(R.id.right_top).setVisibility(View.GONE);
			fragmentTransaction.commit();
		}

	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			return true;
		case R.id.add_book:
			addBook();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void addBook() {
		final AddBookPrompt dialog = new AddBookPrompt(getActivity());
		Button addBookButton = (Button)dialog.findViewById(R.id.add_book_button);
	    addBookButton.setOnClickListener(new View.OnClickListener() {
	   	
	    	 	public void onClick(View v) {
	    	 		EditText title = (EditText) dialog.findViewById(R.id.addedBookTitle);
	    	 		EditText email = (EditText) dialog.findViewById(R.id.addedBookAuthor);
	    	 		if(Utils.isOnline((LasaLaraActivity)getActivity())){
				   		new AddBookTask((LasaLaraActivity) getActivity()).execute(email.getText().toString(),
				   				title.getText().toString(), ((LasaLaraActivity)getActivity()).getPrefsHelper().getUserKey());
	    	 		}
			   		dialog.dismiss();
			   	}
	     });
	    
	    Button cancelButton = (Button)dialog.findViewById(R.id.cancel_add_book_button);
	    cancelButton.setOnClickListener(new View.OnClickListener() {
	    	public void onClick(View v) {
	    		dialog.dismiss();
	    	}
	    });
	    
		dialog.show();
	}
}
